﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASB.CC.Business.ACR;
using ASBCC.Business;
using System.Text;
using ASBCC.Business.CIS;

namespace ASB.CC.Web.ACR.Queries
{
    public partial class IssueQuery : PagedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.dlCategory.DataSource = CustomerSearchHelper.GetCategory();
                this.dlCategory.DataTextField = "CategoryName";
                this.dlCategory.DataValueField = "CategoryId";
                this.dlCategory.DataBind();
                this.dlCategory.Items.Insert(0, new ListItem("--请选择--", ""));
                //BindDataGrid1(1);
                if (!CommonHelper.IsACRAdmin((this.User as CSPrincipal).Role2s))
                    this.btnExport.Visible = false;
                else
                    this.btnExport.Visible = true;
                where = "";
            }
            this.edsCustomer.Where = where;
        }

        protected override EntityDataSource EntitySource
        {
            get { return this.edsExport; }
        }

        protected static string where;
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.GVCustomer.PageIndex = 0;
            this.txtID.Text = PageSafeHelper.SafeText(this.txtID.Text);
            this.txtACR.Text = PageSafeHelper.SafeText(this.txtACR.Text);
            this.txtName.Text = PageSafeHelper.SafeText(this.txtName.Text);
            this.txtCreateBy.Text = PageSafeHelper.SafeText(this.txtCreateBy.Text);
            this.dlCategory.SelectedValue = PageSafeHelper.SafeText(this.dlCategory.SelectedValue);
            var id = this.txtID.Text;
            var acr = this.txtACR.Text;
            var name = this.txtName.Text;
            var creator = this.txtCreateBy.Text;
            var category = this.dlCategory.SelectedValue;
            where = "it.IssueTitle like \"%" + name + "%\" and it.DisplayName like \"%" + creator + "%\"";
            if (!String.IsNullOrEmpty(id) || !String.IsNullOrEmpty(acr))
            {
                string issueids = CustomerSearchHelper.GetIssuesByConditions(id, acr);
                if(!String.IsNullOrEmpty(issueids)&&issueids.Length<10000)
                    where += " and it.IssueId in {" + issueids + "}";
            }
            if (!String.IsNullOrEmpty(category))
                where += " and it.CategoryId=" + category;
            
            this.edsExport.Where = where;
            this.edsExport.DataBind();
            this.GVCustomer.DataBind();

        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.GVCustomer.PageIndex = 0;
            this.txtID.Text = "";
            this.txtACR.Text = "";
            this.txtName.Text = "";
            this.txtCreateBy.Text = "";
            this.dlCategory.SelectedIndex = 0;
            this.edsExport.Where = "";
            where = "";
            this.edsExport.DataBind();
            this.GVCustomer.DataBind();
        }

        protected string GetUserNameById(string id)
        {
            return CustomerSearchHelper.GetUserNameByID(id);
        }
        protected string GetCategoryNameById(string id)
        {
            return CustomerSearchHelper.GetCategoryNameByID(id);
        }


        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Charset = "UTF-8";
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("MyCompany.xls", Encoding.UTF8).ToString());
            //HttpContext.Current.Response.ContentType = "application/ms-excel";
            edsExport.Where = where;
            edsExport.DataBind();
            this.GVExport.Visible = true;
            this.GVExport.DataBind();
            BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(this.Page, this.GVExport, "IssueList.xls");
            //System.IO.StringWriter tw = new System.IO.StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(tw);
            //this.GVExport.RenderControl(hw);
            //HttpContext.Current.Response.Write(tw.ToString());
            //HttpContext.Current.Response.End();
            this.GVExport.Visible = false;
        }

        protected string GetIssueFieldValue(int issueID, int fieldID)
        {
            return CustomerSearchHelper.GetCustomerFieldValue(issueID, fieldID);
        }

        
    }
}
